/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.delivery.validation;

import com.mycompany.delivery.dto.ContactDTO;
import com.mycompany.delivery.dto.DeliveryDTO;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 *
 * @author mhunek
 */
public class DeliveryDTOValidation implements ConstraintValidator<DeliveryDTOConstraint, DeliveryDTO> {

    @Override
    public void initialize(DeliveryDTOConstraint constraintAnnotation) {
       
    }

   @Override
    public boolean isValid(DeliveryDTO deliveryDTO, ConstraintValidatorContext ctx) {
       if(deliveryDTO==null) {
        return false;
       }
       if(deliveryDTO.getPrice() ==0) {
           return false;
       }
        
//here is the actual validation check
       
        
        boolean isValid = deliveryDTO.getPrice()>0;

        //map the error to the price property
        if ( !isValid ) {
            ctx.disableDefaultConstraintViolation();
            ctx.buildConstraintViolationWithTemplate("chyba")
                    .addPropertyNode("name").addConstraintViolation();
        }
        return isValid;
    }
    
}
